package models;

import play.*;
import play.data.validation.MaxSize;
import play.db.jpa.*;
import javax.persistence.*;

import flexjson.JSONSerializer;

import java.util.*;

/**  廉洁风险防控
 * SupervisionList entity managed    廉洁风险防控考评项目
 *  
 *  职务权力监控
 *      决策权
 *      基建权
 *      采购权
 *      人事权
 *      财务权
 *  职业权力监控
 *  供应商诚信管理
 *  患者满意度
 *  党务公开
 *  医德考评 
 */

@Entity 
@Table(name="LZJD_SupervisionList")
public class SupervisionList extends Model {

    public String title;               //考评项目    

    @MaxSize(3000)
    public String evaluationCriteria;  //考评标准

    @MaxSize(1000)
    public String evaluationMethod;   //考评办法

    @MaxSize(100)
    public String evaluationObjects;  //考评对象

    @MaxSize(3000)
    public String ratingCriteria;     //评分标准

    public float score;               //分值

    public String orderNum;
       	
    @MaxSize(1000)
    @Column(name="remark", length=1000)
    public String remark;
    
    @ManyToOne
	@JoinColumn(name = "parent_id")       
	public SupervisionList parentId;

    @Transient
    public long parent;

    public long getParent() {
        return (parentId == null)? 0:parentId.id;
    }
    
    @Transient
    public String parentTitle;

    public String getParentTitle() {
        return (parentId == null)? "":parentId.title;
    }
    
    @OneToMany(mappedBy = "parentId")
    public List<SupervisionList> supervisionLists;
  
    public String toString() {
        return title;
    }

    public static SupervisionList findByTitle(String title) {
        return find("byTitle", title).first();
    }
    
    public static String toJson(List<SupervisionList> supervisionLists) {
    	return new JSONSerializer()
    	  .include("id",  "title", "score", "parentId", "orderNum", "remark", 
    			   "evaluationCriteria", "evaluationMethod", "ratingCriteria",
    			   "supervisionLists.*")
    	  .exclude("*")
    	  .serialize(supervisionLists);    	
    }
    
    public static String toSimpleJson(List<SupervisionList> supervisionLists) {
    	return new JSONSerializer()
    	  .include("id",  "title", "parent")
    	  .exclude("*")
    	  .serialize(supervisionLists);
    }
    
    public static String toLeafJson(List<SupervisionList> supervisionLists) {
    	return new JSONSerializer()
    	  .include("id",  "title", "score",  "orderNum", "parent", "parentTitle")
    	  .exclude("*")
    	  .serialize(supervisionLists);
    }

    public static List<SupervisionList> findTop() {           
        return find("parentId=null").fetch();
    }

    public static List<SupervisionList> findLevel2(long id) {           
        SupervisionList sv = findById(id);
        return sv.supervisionLists;
    }

   
}